package com.priox.sigar.jsf;

import com.mm.support.mapper.DTOMapperService;
import com.priox.sigar.application.checkpoint.RegistroCheckPointApp;
import com.priox.sigar.application.mappers.CheckPointMapper;
import com.priox.sigar.application.mappers.GrupoEmpresarialMapper;
import com.priox.sigar.application.mappers.UsuarioMapper;
import com.priox.sigar.client.dto.CheckPointDTO;
import com.priox.sigar.domain.model.CheckPoint;
import com.priox.sigar.dto.CheckPointFlt;
import com.priox.sigar.enums.ProcesoEstado;
import com.priox.sigar.enums.TipoCuenta;
import com.priox.sigar.jsf.util.FindCrudBeanBase;
import org.primefaces.event.CloseEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;
import java.util.*;


@ManagedBean
@Scope("session")
@Component
public class MaestroCheckPointJsfBean extends FindCrudBeanBase {

    CheckPointFlt filtroBusqueda;

    List<CheckPointDTO> registros = new ArrayList<CheckPointDTO>();

    @Autowired
    UsuarioMapper usuarioMapper;

    @Autowired
    CheckPointMapper checkPointMapper;

    @Autowired
    GrupoEmpresarialMapper grupoEmpresarialMapper;

    @Autowired
    DTOMapperService mapperService;

    @Autowired
    RegistroCheckPointApp registroCheckPointApp;

    @PostConstruct
    public void init() {
        filtroBusqueda = new CheckPointFlt();
        findRegistros();
    }

    /*Métodos de herencia*/

    @Override
    public void setCursorTableNull() {

    }

    @Override
    public void onNewRecord(ActionEvent actionEvent) {
        setUpdateMode(false);
    }

    @Override
    public void onFind(ActionEvent actionEvent) {
        findRegistros();
    }

    public void findRegistros() {
        registros = checkPointMapper.findLast(filtroBusqueda);
        if (registros.size() == 0) {
            addMessage(noRecordFoundMsg, FacesMessage.SEVERITY_INFO);
        }
    }

    @Override
    protected void onSelectAll(ActionEvent actionEvent) {
        for (CheckPointDTO item : registros) {
            item.setSeleccionado(Boolean.TRUE);
        }
    }

    public boolean isSelectRow() {
        for (CheckPointDTO item : registros) {
            if (item.getSeleccionado()) {
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onUnSelectAll(ActionEvent actionEvent) {
        for (CheckPointDTO item : registros) {
            item.setSeleccionado(Boolean.FALSE);
        }
    }

    public TipoCuenta[] getAllTipoCuenta() {
        TipoCuenta[] result = new TipoCuenta[3];
        result[0] = TipoCuenta.CUENTA_ALMACEN;
        result[1] = TipoCuenta.CUENTA_X_PAGAR;
        result[2] = TipoCuenta.CUENTA_X_PAGAR;
        return result;

    }

    public ProcesoEstado[] getAllEstado() {
        return ProcesoEstado.values();

    }

    @Override
    public void onClean(ActionEvent actionEvent) {
        registros = new ArrayList<CheckPointDTO>();
        filtroBusqueda = new CheckPointFlt();
    }

    @Override
    public void onPersist(ActionEvent actionEvent) {

    }

    @Override
    public void initForm(ActionEvent actionEvent) {

    }

    @Override
    public void onShowEditDialog(ActionEvent actionEvent) {

    }

    @Override
    protected void onDeleteSelect(ActionEvent actionEvent) {
        List<CheckPointDTO> remove = new ArrayList<CheckPointDTO>();
        for (CheckPointDTO item : registros) {
            if (item.getSeleccionado()) {
                CheckPoint checkPoint = mapperService.map(item, CheckPoint.class);
                registroCheckPointApp.delete(checkPoint);
                remove.add(item);
            }
        }
        registros.removeAll(remove);
    }


    @Override
    public void onDelete(ActionEvent actionEvent) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected void onDialogClose(CloseEvent event) {

    }




    /*Getters and Setters*/

    public CheckPointFlt getFiltroBusqueda() {
        return filtroBusqueda;
    }

    public void setFiltroBusqueda(CheckPointFlt filtroBusqueda) {
        this.filtroBusqueda = filtroBusqueda;
    }

    public List<CheckPointDTO> getRegistros() {
        return registros;
    }

    public void setRegistros(List<CheckPointDTO> registros) {
        this.registros = registros;
    }
}
